/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureUtilities;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;

import edu.pku.sei.modeler.uml.ResourceFactory;

/**
 * @generated
 *
 */
public class InstanceSpecificationFigure extends CommonFigure {
	/**
	 * 
	 */
	protected static final int BORDER_MARGIN = 5;
	
	private Label nameLabel;

	/**
	 * The border figure.
	 */
	protected BorderFigure borderFigure;

	/**
	 */
	protected Figure contentPane;

	/**
	 */
	protected ContainerFigure slotsContent;

	/**
	 */
	protected boolean isBorderRequired = false;

	/**
	 * The is name underlined.
	 */
	protected boolean isNameUnderlined = false;

	/**
	 * The Class ActorLabelFigure.
	 * Holds the names and stereotypes.
	 */
	protected class BorderFigure extends Figure {
	
		/**
		 * Paint border.
		 * 
		 * @param graphics the graphics
		 */
		@Override
		public void paintBorder(Graphics graphics){
			if (isBorderRequired){
				//setForegroundColor(getBorderColor());
				Point topLeft = getBounds().getTopLeft();
				Point topRight = getBounds().getTopRight();
				graphics.drawLine(topLeft.x,topLeft.y + BORDER_MARGIN,topRight.x,topRight.y + BORDER_MARGIN);
			}	
		}
	
		/**
		 * The Constructor.
		 */
		public BorderFigure() {
			super();
			setSize(new Dimension(10,2*BORDER_MARGIN));
		}
	}


	/**
	 * The Constructor.
	 */
	public InstanceSpecificationFigure() {
		//super();
		//setBorder(new edu.pku.sei.modeler.uml.test.FragmentBorder());
		ToolbarLayout layout = new ToolbarLayout(ToolbarLayout.VERTICAL);
		layout.setStretchMinorAxis(true);
		layout.setMinorAlignment(ToolbarLayout.ALIGN_TOPLEFT);
		setLayoutManager(layout);
		setOpaque(true);
		setBorder(new LineBorder(1));
		
		setForegroundColor(ColorConstants.blue);
		
		nameLabel = new Label();
		nameLabel.setForegroundColor(ColorConstants.black);
		nameLabel.setFont(ResourceFactory.getHeadFont());
		add(nameLabel);
		borderFigure = new BorderFigure();
		borderFigure.setForegroundColor(ColorConstants.blue);
		add(borderFigure);
		setNameUnderlined(true);
		createContentPane();
	}
	
//	/**
//	 * Refresh colors.
//	 * 
//	 * @param graphElement the graph element
//	 */
//	@Override
//	public void refreshColors(GraphElement graphElement) {
//		super.refreshColors(graphElement);
//		
//		Font baseFont = super.getNameLabel().getFont();
//		if (baseFont.getFontData() != null){
//			FontData baseFontData = baseFont.getFontData()[0];
//			FontData[] labelFontData={new FontData(baseFontData.getName(), baseFontData.getHeight(), SWT.BOLD)};
//			Font labelFont = PapyrusPlugin.fontManager.get(labelFontData);	
//			super.setFont(labelFont);
//		}
//		
//		borderFigure.setForegroundColor(getBorderColor());
//	}

	/**
	 * 
	 * 
	 * @return 
	 */
	public BorderFigure getBorderFigure() {
		return borderFigure;
	}

	/**
	 * 
	 */
	protected void createContentPane() {
		contentPane = new Figure();
		contentPane.setForegroundColor(ColorConstants.blue);
		contentPane.setBackgroundColor(ColorConstants.blue);
		ToolbarLayout layout = new ToolbarLayout();
		layout.setVertical(true);
		contentPane.setLayoutManager(layout);
		add(contentPane);	
	}
	
	public void setName( String name )
	{
	
		this.nameLabel.setText(name);
	}

//	/**
//	 * 
//	 * 
//	 * @param b 
//	 */
//	public void setAbstract(boolean b){
//		Font baseFont = super.getNameLabel().getFont();
//	
//		if (b) {
//			if (baseFont.getFontData() != null){
//				FontData baseFontData = baseFont.getFontData()[0];
//				FontData[] labelFontData={new FontData(baseFontData.getName(), baseFontData.getHeight(), SWT.ITALIC|SWT.BOLD)};
//				Font labelFont = PapyrusPlugin.fontManager.get(labelFontData);	
//				super.setFont(labelFont);
//			}
//		} else {
//			if (baseFont.getFontData() != null){
//				FontData baseFontData = baseFont.getFontData()[0];
//				FontData[] labelFontData={new FontData(baseFontData.getName(), baseFontData.getHeight(), SWT.BOLD)};
//				Font labelFont = PapyrusPlugin.fontManager.get(labelFontData);	
//				super.setFont(labelFont);
//			}
//		}
//	}

	/**
	 * 
	 * 
	 * @param isBorderRequired 
	 */
	public void setBorderRequired(boolean isBorderRequired) {
		this.isBorderRequired = isBorderRequired;
	}

	/**
	 * 
	 * 
	 * @param rect 
	 */
	@Override
	public void setBounds(Rectangle rect) {
		super.setBounds(rect);
		Dimension labelFigureDimension = getNameLabel().getPreferredSize();
		Dimension borderFigureDimension = getBorderFigure().getPreferredSize();
		if (slotsContent != null) {
			slotsContent.setPreferredSize(rect.width, rect.height - labelFigureDimension.height - borderFigureDimension.height);
		}
	}

	/**
	 * Paint border.
	 * 
	 * @param graphics the graphics
	 */
	@Override
	public void paintBorder(Graphics graphics){
		super.paintBorder(graphics);
		if (isNameUnderlined) {
			graphics.setForegroundColor(ColorConstants.black);
			Rectangle rectLabel = getNameLabel().getBounds();
			Dimension labelSize=FigureUtilities.getTextExtents(getNameLabel().getText(), getNameLabel().getFont());
			graphics.drawLine(rectLabel.x+rectLabel.width/2-labelSize.width/2,
					rectLabel.y+rectLabel.height+1,
					rectLabel.x+rectLabel.width/2+labelSize.width/2,
					rectLabel.y+rectLabel.height+1);
		}
	}

	public Label getNameLabel() {
		return this.nameLabel;
	}

	/**
	 * 
	 * 
	 * @param isNameUnderlined 
	 */
	public void setNameUnderlined(boolean isNameUnderlined) {
		this.isNameUnderlined = isNameUnderlined;
	}

	/**
	 * Get the inner figure accepting childs figure.
	 * 
	 * @return 
	 */
	public IFigure getContentPane() {
		return contentPane;	
	}
}
